﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class DonDatHangDAO
    {
        public static bool Insert(DonDatHangDTO ddh)
        {
            bool result = false;

            string strSQLEmployee = "Insert into DonDatHang(NguoiLap,NgayLap,TriGia,PhieuSuDungNVL,NCC) " +
                "values (@Nguoi,@Ngay,@Tri,@Phieu,@NCC)";

            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@Nguoi", ddh.NguoiLap));
                sqlParams.Add(new SqlParameter("@Ngay", ddh.NgayLap));
                sqlParams.Add(new SqlParameter("@Tri", ddh.TriGia));
                sqlParams.Add(new SqlParameter("@Phieu", ddh.PhieuSuDungNVL));
                sqlParams.Add(new SqlParameter("@NCC", ddh.MaNCC));
                // Call execute none query

                int n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);//.ExecuteNoneQuery(strSQLEmployee, sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool Update(DonDatHangDTO ddh)
        {
            bool result = false;

            string strSQLEmployee = "Update DonDatHang set TriGia=@Tri where MaDDH=@MaDDH";

            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaDDH", ddh.MaDDH));
                sqlParams.Add(new SqlParameter("@Tri", ddh.TriGia));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static int Select_MaDDH_Max()
        {
            int MaDDH_max = 0;
            String strSql = "select Max(MaDDH) as MaDDH from DonDatHang";

            try
            {
                DataTable dt = SqlDataAccessHelper.ExcuteQuery(strSql);
                DataRow dr = dt.Rows[0];
                MaDDH_max = int.Parse(dr["MaDDH"].ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return MaDDH_max;
        }
    }
}
